﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace JqueryNet.FormControl.FormWebControl
{
    public class JTreeView : JWebControl
    {
        TreeView tree;
        public JTreeView(TreeView tree) : base(tree) { this.tree = tree; }

        public override void Val()
        {
            Value = tree.SelectedNode.Value;
        }

        public override void Val(string value)
        {
            foreach (TreeNode item in tree.Nodes)
            {
                if (item.Value == value)
                {
                    item.Selected = true;
                    return;
                }
                SelectNode(item, value);
            }
        }

        private void SelectNode(TreeNode item, string value)
        {
            foreach (TreeNode citem in item.ChildNodes)
            {
                if (citem.Value == value)
                {
                    citem.Selected = true;
                    return;
                }
                SelectNode(citem, value);
            }                    
        }

        public override void Val(string[] values)
        {
            base.Val(values);
        }
    }
}
